package com.imti.mall.sale.controller;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.sms.pojo.SmsHomeNewProduct;
import com.imti.mall.model.sms.pojo.SmsHomeRecommendProduct;
import com.imti.mall.model.sms.vo.SmsHomeNewProductVo;
import com.imti.mall.model.sms.vo.SmsHomeRecommendProductPicVo;
import com.imti.mall.model.sms.vo.SmsHomeRecommendProductVo;
import com.imti.mall.sale.mapper.SmsHomeRecommendProductMapper;
import com.imti.mall.sale.service.SmsHomeRecommendProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "人气推荐商品表")
@RequestMapping("/api/v1/sale")
public class SmsHomeRecommendProductController  {

    @Autowired
    SmsHomeRecommendProductService smsHomeRecommendProductService;
    /**
     * 查询人气推荐商品表
     */
    @ApiOperation(value = "查询人气推荐商品表")
    @PostMapping(value = "/findSmsHomeRecommendProduct" )
    public CommonResult findSmsHomeRecommendProduct
    ( SmsHomeRecommendProduct smsHomeRecommendProduct,
      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<SmsHomeRecommendProduct> list = smsHomeRecommendProductService.findSmsHomeRecommendProduct(smsHomeRecommendProduct,pageSize, pageNum);
        if(list.size()>0){
            return CommonResult.success(CommonPage.restPage(list));
        }else {
            return CommonResult.success("未查询到信息");
        }
    }

    /**
     * 添加人气推荐商品表
     */
    @ApiOperation(value = "添加人气推荐商品表")
    @PostMapping(value = "/addSmsHomeRecommendProduct" )
    public CommonResult addSmsHomeRecommendProduct(@RequestBody List<SmsHomeRecommendProduct> smsHomeRecommendProductList) {
        Integer res=0;
        for(int i=0;i<smsHomeRecommendProductList.size();i++){
            res= smsHomeRecommendProductService.addSmsHomeRecommendProduct(smsHomeRecommendProductList.get(i));
        }
        if(res>0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 修改人气推荐商品表
     */
    @ApiOperation(value = "修改人气推荐商品表")
    @PostMapping(value = "/updateSmsHomeRecommendProduct" )
    public CommonResult updateSmsHomeRecommendProduct(SmsHomeRecommendProductVo smsHomeRecommendProductVo) {
        int res = 0;
        if (smsHomeRecommendProductVo.getId() != null) {
            SmsHomeRecommendProduct smsHomeRecommendProduct = smsHomeRecommendProductVo;
            res = smsHomeRecommendProductService.updateSmsHomeRecommendProduct(smsHomeRecommendProductVo);
        } else {
            Long[] ids = smsHomeRecommendProductVo.getIds();
            for (int i = 0; i < ids.length; i++) {
                SmsHomeRecommendProduct smsHomeRecommendProduct = smsHomeRecommendProductVo;
                smsHomeRecommendProduct.setId(ids[i]);
                res = smsHomeRecommendProductService.updateSmsHomeRecommendProduct(smsHomeRecommendProduct);
            }
        }
        if (res > 0) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }


    /**
     * 删除人气推荐商品表
     */
    @ApiOperation(value = "删除人气推荐商品表")
    @PostMapping(value = "/delSmsHomeRecommendProduct" )
    public CommonResult delSmsHomeRecommendProduct( SmsHomeRecommendProductVo smsHomeRecommendProductVo) {
        Long[] ids= smsHomeRecommendProductVo.getIds();
        int res =0;
        for(int i=0;i<ids.length;i++){
            res = smsHomeRecommendProductService.delSmsHomeRecommendProduct(ids[i].intValue());
        }
        if(res>0){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * App查询人气图片
     * @return
     */
    @ApiOperation(value = "App查询人气图片")
    @PostMapping(value = "/findSmsHomeRecommendProductPic" )
    public CommonResult findSmsHomeRecommendProductPic(SmsHomeRecommendProductPicVo smsHomeRecommendProductPicVo) {
        List<SmsHomeRecommendProductPicVo> list = smsHomeRecommendProductService.findSmsHomeRecommendProductPic(smsHomeRecommendProductPicVo);

        return CommonResult.success(list);
    }

}
